package com.baihan.angus.app.game.busshow.pop_dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.utils.AppUtil;
import com.baihan.angus.app.game.basic.utils.ComUtil;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.adps.ClueDialogTitleAdp;
import com.baihan.angus.app.game.busshow.adps.DmClueAdp;
import com.baihan.angus.app.game.busshow.adps.DmManualAdp;
import com.baihan.angus.app.game.busshow.adps.DmMapAdp;
import com.baihan.angus.app.game.busshow.adps.DmPropsAdp;
import com.baihan.angus.app.game.busshow.adps.ScriptAdp;
import com.baihan.angus.app.game.common.bean.entity.ClueEntity;
import com.baihan.angus.app.game.common.bean.entity.MapEntity;
import com.baihan.angus.app.game.common.bean.response.DmClueResponse;
import com.baihan.angus.app.game.common.bean.response.DmManualResponse;
import com.baihan.angus.app.game.common.bean.response.DmMapResponse;
import com.baihan.angus.app.game.common.bean.response.DmPropsResponse;
import com.baihan.angus.app.game.common.bean.response.DmScriptResponse;
import com.baihan.angus.app.game.common.bean.response.ManualMenuResponse;
import com.zzhoujay.richtext.RichText;

import java.util.List;
import java.util.stream.Collectors;

import static com.baihan.angus.app.game.busshow.pop_dialog.DmScriptDialog.MANUAL_MENU_CHECK;

public class DmManualDialog extends Dialog {

    private Callback mCallback;
    private Context mContext;
    private WindowManager.LayoutParams mLayoutParams;
    private TextView tvTitle;
    private ImageView ivClose;
    private View viewLine;
    private RecyclerView rvTitle, rvDatas;
    private LinearLayout llMenu, llNoData;

    private DmManualAdp mDmManualAdp;
    private ScriptAdp mScriptAdp;
    private DmClueAdp mDmClueAdp;
    private DmPropsAdp mDmPropsAdp;
    private DmMapAdp mDmMapAdp;

    private ClueDialogTitleAdp mClueDialogTitleAdp;
    private List<String> bottomTitleList;
    private List<String> leftTitleList;
    private List<DmManualResponse.DataX> dmManualList;
    private List<DmScriptResponse.DataXX> dmScriptList;
    private List<DmClueResponse.DataX> dmClueList;
    private List<DmPropsResponse.DataX> dmPropsList;
    private List<DmMapResponse.DataX> dmMapList;

    private ManualPopup mManualPopup;

    private int selectPosition = 0;

    public DmManualDialog(@NonNull Context context, Callback callback) {
        super(context, R.style.FullDialog);
        this.mContext = context;
        this.mCallback = callback;
        initContentView();
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
        mLayoutParams.height = (int) (AppUtil.getScreenHeight(context) * 0.8);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.getDecorView().setBackgroundResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_dm_manual, (ViewGroup) null);
        setContentView(dialogView);
        tvTitle = dialogView.findViewById(R.id.tvTitle);
        rvTitle = dialogView.findViewById(R.id.rvTitle);
        rvDatas = dialogView.findViewById(R.id.rvDatas);
        ivClose = dialogView.findViewById(R.id.ivClose);
        viewLine = dialogView.findViewById(R.id.viewLine);
        llNoData = dialogView.findViewById(R.id.llNoData);
        llMenu = dialogView.findViewById(R.id.llMenu);

        ivClose.setOnClickListener(v -> {
            RichText.clear(this);
            cancel();
        });

        mClueDialogTitleAdp = new ClueDialogTitleAdp();
        rvTitle.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        mClueDialogTitleAdp.bindToRecycleView(rvTitle);

        mClueDialogTitleAdp.setOnItemClickListener(new ClueDialogTitleAdp.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                mClueDialogTitleAdp.setThisPosition(position);
                mClueDialogTitleAdp.notifyDataSetChanged();
                if (selectPosition == 0) {
                    mDmManualAdp.clear();
                    if (null != dmManualList.get(position).getData() && dmManualList.get(position).getData().size() > 0) {
                        rvDatas.setVisibility(View.VISIBLE);
                        llNoData.setVisibility(View.GONE);
                        mDmManualAdp.addAll(dmManualList.get(position).getData());
                    } else {
                        rvDatas.setVisibility(View.GONE);
                        llNoData.setVisibility(View.VISIBLE);
                    }
                } else if (selectPosition == 1) {
                    mScriptAdp.clear();
                    if (null != dmScriptList.get(position).getData() && dmScriptList.get(position).getData().size() > 0) {
                        rvDatas.setVisibility(View.VISIBLE);
                        llNoData.setVisibility(View.GONE);
                        mScriptAdp.addAll(dmScriptList.get(position).getData());
                    } else {
                        rvDatas.setVisibility(View.GONE);
                        llNoData.setVisibility(View.VISIBLE);
                    }
                } else if (selectPosition == 2) {
                    mDmClueAdp.clear();
                    if (null != dmClueList.get(position).getData() && dmClueList.get(position).getData().size() > 0) {
                        rvDatas.setVisibility(View.VISIBLE);
                        llNoData.setVisibility(View.GONE);
                        mDmClueAdp.addAll(dmClueList.get(position).getData());
                    } else {
                        rvDatas.setVisibility(View.GONE);
                        llNoData.setVisibility(View.VISIBLE);
                    }
                } else if (selectPosition == 3) {
                    mDmPropsAdp.clear();
                    if (null != dmPropsList.get(position).getData() && dmPropsList.get(position).getData().size() > 0) {
                        rvDatas.setVisibility(View.VISIBLE);
                        llNoData.setVisibility(View.GONE);
                        mDmPropsAdp.addAll(dmPropsList.get(position).getData());
                    } else {
                        rvDatas.setVisibility(View.GONE);
                        llNoData.setVisibility(View.VISIBLE);
                    }
                } else if (selectPosition == 4) {
                    mDmMapAdp.clear();
                    if (null != dmMapList.get(position).getData() && dmMapList.get(position).getData().size() > 0) {
                        rvDatas.setVisibility(View.VISIBLE);
                        llNoData.setVisibility(View.GONE);
                        mDmMapAdp.addAll(dmMapList.get(position).getData());
                    } else {
                        rvDatas.setVisibility(View.GONE);
                        llNoData.setVisibility(View.VISIBLE);
                    }
                }
            }
        });

        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 2);
        mDmManualAdp = new DmManualAdp();
        rvDatas.setLayoutManager(gridLayoutManager);

        mDmManualAdp.setOnItemClickListener(new DmManualAdp.OnItemClickListener() {
            @Override
            public void onView(int position) {
                if (null != mCallback) {
                    //切换item先将menu位置设为选中的position
                    MANUAL_MENU_CHECK = position;
                    mCallback.onShowDetail(MANUAL_MENU_CHECK, dmManualList.get(0).getData());
                }
            }
        });

        mScriptAdp = new ScriptAdp();
        rvDatas.setLayoutManager(gridLayoutManager);

        mScriptAdp.setOnItemClickListener(new ScriptAdp.OnItemClickListener() {
            @Override
            public void onSend(int position) {
                if (null != mCallback) {
                    mCallback.onSendScript(position, mScriptAdp.getItem(position).getData().getSn());
                }
            }
        });

        mDmClueAdp = new DmClueAdp();
        rvDatas.setLayoutManager(gridLayoutManager);

        mDmClueAdp.setOnItemClickListener(new DmClueAdp.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (null != mCallback) {
                    mCallback.onShowClueDetail(position, mDmClueAdp.getItem(position));
                }
            }

            @Override
            public void onSend(int position) {
                if (null != mCallback) {
                    mCallback.onSendClue(position, mDmClueAdp.getItem(position).getScript_relevant_id());
                }
            }

            @Override
            public void onOpen(int position) {
                if (null != mCallback) {
                    mCallback.onOpenClue(position, mDmClueAdp.getItem(position).getScript_relevant_id());
                }
            }
        });

        mDmPropsAdp = new DmPropsAdp();
        rvDatas.setLayoutManager(gridLayoutManager);

        mDmPropsAdp.setOnItemClickListener(new DmPropsAdp.OnItemClickListener() {
            @Override
            public void onSend(int position) {
                if (null != mCallback) {
                    mCallback.onSendProps(position, mDmPropsAdp.getItem(position).getScript_relevant_id());
                }
            }
        });

        mDmMapAdp = new DmMapAdp();
        rvDatas.setLayoutManager(gridLayoutManager);

        mDmMapAdp.setOnItemClickListener(new DmMapAdp.OnItemClickListener() {
            @Override
            public void onShow(int position) {
                if (null != mCallback) {
                    mCallback.onShowMap(position, mDmMapAdp.getItem(position).getScript_relevant_id());
                }
            }

            @Override
            public void onItemClick(int positon) {
                if (null != mCallback) {
                    mCallback.onShowMapDetail(positon, mDmMapAdp.getItem(positon));
                }
            }
        });

        ivClose.setOnClickListener(v -> {
            cancel();
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void initData(ManualMenuResponse.Data menu, List<DmManualResponse.DataX> manualList, List<DmScriptResponse.DataXX> scriptList,
                         List<DmClueResponse.DataX> clueList, List<DmPropsResponse.DataX> propsList, List<DmMapResponse.DataX> mapList) {
        dmManualList = manualList;
        dmScriptList = scriptList;
        dmClueList = clueList;
        dmPropsList = propsList;
        dmMapList = mapList;

        //该方法6.0不兼容
        //总的底部目录
        bottomTitleList = menu.getManual_data().stream().map(ManualMenuResponse.Data.ManualData::getTitle).collect(Collectors.toList());

        setData(selectPosition, menu);

        llMenu.setOnClickListener(v -> {
            mManualPopup = new ManualPopup(mContext, bottomTitleList, selectPosition, new ManualPopup.OnSelectListener() {
                @Override
                public void onSelect(int position) {
                    selectPosition = position;
                    setData(position, menu);

                    mManualPopup.dismiss();
                }
            });
            int[] location = new int[2];
            llMenu.getLocationInWindow(location);
            if (!mManualPopup.isShowing()) {
                mManualPopup.showAtLocation(llMenu, Gravity.NO_GRAVITY,
                        location[0], location[1] - ComUtil.Dp2Px(mContext, 194));
            }
        });
    }

    private void setData(int position, ManualMenuResponse.Data menu) {
        tvTitle.setText(menu.getManual_data().get(position).getTitle());
        mClueDialogTitleAdp.clear();
        mDmManualAdp.clear();
        mScriptAdp.clear();
        mDmClueAdp.clear();
        mDmPropsAdp.clear();
        mDmMapAdp.clear();
        if (position == 0) {
            rvDatas.setAdapter(mDmManualAdp);
            //左侧目录
            if (null != dmManualList && dmManualList.size() > 0) {
                rvTitle.setVisibility(View.VISIBLE);
                rvDatas.setVisibility(View.VISIBLE);
                viewLine.setVisibility(View.VISIBLE);
                llNoData.setVisibility(View.GONE);
                leftTitleList = dmManualList.stream().map(DmManualResponse.DataX::getTitle).collect(Collectors.toList());
                mDmManualAdp.addAll(dmManualList.get(0).getData());
            } else {
                rvTitle.setVisibility(View.GONE);
                rvDatas.setVisibility(View.GONE);
                viewLine.setVisibility(View.GONE);
                llNoData.setVisibility(View.VISIBLE);
            }
        } else if (position == 1) {
            rvDatas.setAdapter(mScriptAdp);
            if (null != dmScriptList && dmScriptList.size() > 0) {
                rvTitle.setVisibility(View.VISIBLE);
                rvDatas.setVisibility(View.VISIBLE);
                viewLine.setVisibility(View.VISIBLE);
                llNoData.setVisibility(View.GONE);
                leftTitleList = dmScriptList.stream().map(DmScriptResponse.DataXX::getTitle).collect(Collectors.toList());
                mScriptAdp.addAll(dmScriptList.get(0).getData());
            } else {
                rvTitle.setVisibility(View.GONE);
                rvDatas.setVisibility(View.GONE);
                viewLine.setVisibility(View.GONE);
                llNoData.setVisibility(View.VISIBLE);
            }
        } else if (position == 2) {
            rvDatas.setAdapter(mDmClueAdp);
            if (null != dmClueList && dmClueList.size() > 0) {
                rvTitle.setVisibility(View.VISIBLE);
                rvDatas.setVisibility(View.VISIBLE);
                viewLine.setVisibility(View.VISIBLE);
                llNoData.setVisibility(View.GONE);
                leftTitleList = dmClueList.stream().map(DmClueResponse.DataX::getTitle).collect(Collectors.toList());
                mDmClueAdp.addAll(dmClueList.get(0).getData());
            } else {
                rvTitle.setVisibility(View.GONE);
                rvDatas.setVisibility(View.GONE);
                viewLine.setVisibility(View.GONE);
                llNoData.setVisibility(View.VISIBLE);
            }
        } else if (position == 3) {
            rvDatas.setAdapter(mDmPropsAdp);
            if (null != dmPropsList && dmPropsList.size() > 0) {
                rvTitle.setVisibility(View.VISIBLE);
                rvDatas.setVisibility(View.VISIBLE);
                viewLine.setVisibility(View.VISIBLE);
                llNoData.setVisibility(View.GONE);
                leftTitleList = dmPropsList.stream().map(DmPropsResponse.DataX::getTitle).collect(Collectors.toList());
                mDmPropsAdp.addAll(dmPropsList.get(0).getData());
            } else {
                rvTitle.setVisibility(View.GONE);
                rvDatas.setVisibility(View.GONE);
                viewLine.setVisibility(View.GONE);
                llNoData.setVisibility(View.VISIBLE);
            }
        } else if (position == 4) {
            rvDatas.setAdapter(mDmMapAdp);
            if (null != dmMapList && dmMapList.size() > 0) {
                rvTitle.setVisibility(View.VISIBLE);
                rvDatas.setVisibility(View.VISIBLE);
                viewLine.setVisibility(View.VISIBLE);
                llNoData.setVisibility(View.GONE);
                leftTitleList = dmMapList.stream().map(DmMapResponse.DataX::getTitle).collect(Collectors.toList());
                mDmMapAdp.addAll(dmMapList.get(0).getData());
            } else {
                rvTitle.setVisibility(View.GONE);
                rvDatas.setVisibility(View.GONE);
                viewLine.setVisibility(View.GONE);
                llNoData.setVisibility(View.VISIBLE);
            }
        }
        mClueDialogTitleAdp.addAll(leftTitleList);
        mClueDialogTitleAdp.setThisPosition(0);
    }

    public void refreshScript(int position) {
        mScriptAdp.getItem(position).setIs_send(1);
        mScriptAdp.notifyDataSetChanged();
    }

    public void refreshClueSend(int position) {
        mDmClueAdp.getItem(position).setIs_send(1);
        mDmClueAdp.notifyDataSetChanged();
    }

    public void refreshClueOpen(int position) {
        mDmClueAdp.getItem(position).setIs_open(1);
        mDmClueAdp.notifyDataSetChanged();
    }

    public void refreshPropSend(int position, String roleName) {
        mDmPropsAdp.getItem(position).setIs_send(1);
        mDmPropsAdp.getItem(position).setScript_role_name(roleName);
        mDmPropsAdp.getItem(position).setHold_script_role_name(roleName);
        mDmPropsAdp.notifyDataSetChanged();
    }

    public void refreshMapSend(int position) {
        mDmMapAdp.getItem(position).setIs_show(1);
        mDmMapAdp.notifyDataSetChanged();
    }

    public interface Callback {
        void onShowDetail(int selectPos, List<DmManualResponse.DataX.Data> data);

        void onSendScript(int position, String scriptId);

        void onSendClue(int position, String clueId);

        void onOpenClue(int position, String clueId);

        void onShowClueDetail(int position, ClueEntity clueData);

        void onSendProps(int position, String propId);

        void onShowMap(int position, String mapId);

        void onShowMapDetail(int position, MapEntity mapData);
    }
}
